package com.shigu.admin.action;

import com.opentae.data.mall.beans.MallSeller;
import com.shigu.MallResponse;
import com.shigu.PageRequest;
import com.shigu.PageResponse;
import com.shigu.exceptions.AdminException;
import com.shigu.seller.bo.AddSellerRequest;
import com.shigu.seller.bo.ModifySellerRequest;
import com.shigu.seller.bo.SellerIdRequest;
import com.shigu.seller.service.SellerAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("adminjson")
public class SellerController {
    @Autowired
    SellerAdminService sellerAdminService;

    @RequestMapping("addSeller")
    @ResponseBody
    public MallResponse addSeller(@Valid AddSellerRequest request, BindingResult result) throws AdminException {
        if(result.hasErrors()){
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return sellerAdminService.addSeller(request.getSellerName());
    }

    @RequestMapping("sellerList")
    @ResponseBody
    public PageResponse<MallSeller> sellerList(PageRequest request){
        return sellerAdminService.sellerList(request.getPage(),request.getSize());
    }

    @RequestMapping("modifySellerName")
    @ResponseBody
    public MallResponse modifySellerName(@Valid ModifySellerRequest request,BindingResult result) throws AdminException {
        if(result.hasErrors()){
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return sellerAdminService.modifySellerName(request.getSellerId(),request.getSellerName());
    }

    @RequestMapping("deleteSeller")
    @ResponseBody
    public MallResponse deleteSeller(@Valid SellerIdRequest request, BindingResult result) throws AdminException {
        if(result.hasErrors()){
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return sellerAdminService.deleteSeller(request.getSellerId());
    }

    @RequestMapping("sellers")
    @ResponseBody
    public PageResponse<MallSeller> sellers(){
        return sellerAdminService.sellers();
    }
}


